<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="代理名称">
                <div class="flex">
                  <a-input allow-clear placeholder="请输入代理名称" v-model="queryParam.name"></a-input>
                  <a-button type="primary" @click="getList()">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList()">
          <a-radio-button :value="item.id" :key="item.id" v-for="item of statusList"> {{ item.title }} </a-radio-button>
        </a-radio-group>
      </div>

      <a-table
        style="width: 100%; overflow-x: auto"
        size="default"
        :columns="columns"
        :data-source="list"
        :row-key="(record) => record.id"
        :pagination="{
          total: this.total,
          pageSize: this.pageSize,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
      >
        <span slot="useInfo" slot-scope="text, record">
          <div class="content">
            <div class="img">
              <a-avatar shape="square" :size="48" icon="user" :src="record.avatar" />
            </div>

            <div class="txt">
              <a-tooltip>
                <template slot="title"> {{ record.name }} </template>
                <h3>{{ record.name }}</h3>
              </a-tooltip>
              <p>手机号: {{ record.mobile }}</p>
            </div>
          </div>
        </span>

        <span slot="status" slot-scope="text">
          <a-badge :color="getStatus(text)[1]" :text="getStatus(text)[0]" />
        </span>

        <span slot="action" slot-scope="text, record">
          <template v-if="record.status === 0">
            <a-button type="link" size="small" @click="handleItem('status', record, 1)">通过</a-button>
            <a-button type="link" size="small" @click="handleItem('status', record, 2)">拒绝</a-button>
          </template>
          <span v-else>-</span>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { getAgentApplyList, setAgentApplyStatus } from '@/api/auth'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '用户信息',
    dataIndex: 'useInfo',
    scopedSlots: {
      customRender: 'useInfo',
    },
    width: 300,
    ellipsis: true,
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '创建时间',
    dataIndex: 'created_at',
    width: 120,
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
    width: 150,
  },
]

export default {
  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,
      queryParam: {
        name: '',
        status: '-1',
        agent_type: '-1',
      },
      list: [],
      selectedRowKeys: [],
      statusList: [
        { id: '-1', title: '全部' },
        { id: '0', title: '未审核' },
        { id: '1', title: '已通过' },
        { id: '2', title: '已拒绝' },
      ],
    }
  },

  mounted() {
    let { type } = this.$route.meta
    this.queryParam.agent_type = type
    this.getList()
  },

  computed: {
    getStatus() {
      return (status) => {
        let text, color
        switch (String(status)) {
          case '0':
            text = '未审核'
            color = 'rgba(0, 0, 0, 0.25)'
            break

          case '1':
            text = '已通过'
            color = '#65C817'
            break

          case '2':
            text = '已拒绝'
            color = '#FF4D4F'
            break
        }
        return [text, color]
      }
    },
  },

  methods: {
    // 获取数据列表
    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this

      const res = await getAgentApplyList({
        ...queryParam,
        pageSize,
        pageNo,
      })
      if (res.code == 0) {
        this.selectedRowKeys = []
        let { totalCount, data } = res.data
        this.total = totalCount
        this.list = data
      }
      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    // 点击单个
    handleItem(type, item, parmas) {
      switch (type) {
        case 'status':
          this.status({
            id: item.id,
            status: parmas,
          })
          break
      }
    },

    status(data) {
      this.$confirm({
        title: '提示?',
        content: '是否执行此次操作？',
        onOk: async () => {
          const hide = this.$message.loading('请稍等...')
          const res = await setAgentApplyStatus(data)
          hide()
          if (res.code) {
            this.$error({
              content: res.msg,
            })
            return
          }
          this.$message.success(res.msg)
          this.getList()
        },
        onCancel: () => {},
      })
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}

.flex {
  .inp {
    margin-right: 16px;
  }
}
</style>
